/**
 * 
 */
package edu.rice.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

/**
 * @author frogy
 * 
 */
public class TH {
	public TH() {
		initiallize(52);
		for (int i = 0; i <= 10000000; i++) {
			play(i);
		}
	}

	public void initiallize(int cn) {
		// Prepare cards
		card = new Card[cn];
		int k = 0;
		// Suit: Spade, heart, club, diamond
		for (int i = 0; i < 4; i++) {
			for (int j = 1; j <= 13; j++) {
				card[k] = new Card(i, j);
				k++;
			}
		}
		// Prepare players
		int pn = -1;
		System.out.println("How many human players? (max 9)");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			pn = Integer.parseInt(br.readLine());
		} catch (IOException e) {
		}
		System.out.println("Please enter player name");
		player = new Player[N];
		for (int i = 0; i < pn; i++) {
			br = new BufferedReader(new InputStreamReader(System.in));
			String name = null;
			try {
				name = br.readLine();
			} catch (IOException e) {
			}
			player[i] = new Player(name);
		}
		for (int i = pn; i < N; i++) {
			String name = "Player" + i;
			player[i] = new Player(name);
		}
	}

	public void play(int i) {
		shuffleCards(card);
		resetPlayer();
		// Create pool
		pl = new Pool();
		pl.max = 0;
		pl.total = 0;
		player[i % N].sb(pl);
		player[(i + 1) % N].bb(pl);
		sbn = i % N;
		// burn card
		cardindex = 1;
		dealPersonalCard();
		cardindex = 2 * N + 1;
		betting2();
		// judgewinner();
		// checkremainingplayer();
		// dealpubcard(3);
		// betting();
		// judgewinner();
		// checkremainingplayer();
		// dealpubcard(1);
		// betting();
		// judgewinner();
		// checkremainingplayer();
		// dealpubcard(1);
		// betting
		// judgewinner();
	}

	public void dealPersonalCard() {
		for (int i = 0; i < N; i++) {
			Card[] c = new Card[2];
			c[0] = card[i + 1];
			c[1] = card[i + 1 + N];
			player[i].setCard(c);
		}
		// showHand();
	}

	public void betting2() {
		for (int i = 0; i < N; i++) {
			player[(i + sbn) % N].betting2(pl);
		}

	}

	public void resetPlayer() {
		for (int i = 0; i < N; i++) {
			player[i].reset();
		}
	}

	public void shuffleCards(Card[] c) {
		Random r = new Random();
		for (int i = 0; i < 2 * c.length; i++) {
			int ind = r.nextInt(c.length);
			Card temp = c[ind];
			c[ind] = c[c.length - 1];
			c[c.length - 1] = temp;
		}
	}

	public void showHand() {
		for (int i = 0; i < 9; i++) {
			player[i].showHand();
		}
	}

	public static void main(String[] args) {
		new TH();
	}

	Card[] card;

	Player[] player;

	int cardindex;

	Pool pl;

	int N = 9;

	int sbn;

}

class Card {
	int suit;

	int num;

	public Card(int k, int n) {
		suit = k;
		num = n;
	}

	public int getSuit() {
		return suit;
	}

	public int getNum() {
		return num;
	}
}

class Player {
	String name;

	Card[] card;

	int chips;

	int bet;

	boolean fold;

	public Player(String n) {
		name = n;
		chips = 1000;
		fold = false;
	}

	public void sb(Pool pl) {
		chips -= 5;
		pl.total += 5;
		pl.max = 5;
	}

	public void bb(Pool pl) {
		chips -= 10;
		pl.total += 10;
		pl.max = 10;
	}

	public void betting2(Pool pl) {
		bet = 0;
		if (card[0].suit == card[1].suit) {
			bet += 30;
		}
		if (Math.abs(card[0].num - card[1].num) <= 3) {
			bet += 20;
		}
		if (card[0].num == card[1].num) {
			bet += 30;
		}
		int highcard = card[0].num > card[1].num ? card[0].num : card[1].num;
		if (highcard > 7) {
			bet += 10;
		} else if (highcard >= 12) {
			bet += 20;
		}
		if(bet > chips){
			bet = chips;
			if(bet > pl.max)
				pl.max = bet;
		}else if (Math.abs(bet - pl.max) <= 20) {
			bet = pl.max;
		}else if (bet < pl.max) {
			fold = true;
		}else if (bet > pl.max){
			pl.max = bet;
		}
	}

	public void match(Pool pl) {
		
		if (((double) pl.max - (double) bet) / (double) bet < 0.2) {
			if(pl.max < chips){
				bet = pl.max;
			}else {
				bet = chips;
			}
		} else {
			bet = 0;
			fold = true;
		}
	}
	
	//unfinished
	public void pushToPool(Pool pl){
		if(!fold){
			if(bet < pl.max){
				if(bet != chips){
					System.out.println("Somthing is wrong!~");
				}
				if(pl.max1 == 0){
					pl.max1 = bet;
					
				} else if(pl.max2 == 0){
					pl.max2 = bet;
				} else if(pl.max3 == 0){
					pl.max3 = bet;
				}
			}
		}
	}

	public void reset() {
		fold = false;
	}

	public void setCard(Card[] c) {
		card = c;
	}

	public void showHand() {
		System.out.println(card[0].getSuit() + " " + card[0].getNum() + "     "
				+ card[1].getSuit() + " " + card[1].getNum());
	}

	public void showState() {
		System.out.println("Folded? " + fold);
		if (!fold) {
			System.out.println("Player " + name + "'s bet is: " + bet);
		}
	}

	public String getName() {
		return name;
	}
}

class Pool {
	public int total;
	
	public int total1;
	
	public int total2;
	
	public int total3;

	public int max;
	
	public int max1;
	
	public int max2;
	
	public int max3;
}
